import 'package:flutter/material.dart';
import 'package:flutter_color_plugin/flutter_color_plugin.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_slidable/flutter_slidable.dart';
import 'package:yw_bapp/dao/order_dao.dart';
import 'package:yw_bapp/dao/withdrawal_dao.dart';
import 'package:yw_bapp/model/news_list_model.dart';
import 'package:yw_bapp/model/order_list_model.dart';
import 'package:yw_bapp/pages/withdraw_detail.dart';
import 'package:yw_bapp/provider/store_info_provider.dart';
import 'package:yw_bapp/utils/common_class.dart';
import 'package:yw_bapp/utils/timeHandle.dart';
import 'package:yw_bapp/widgets/loading_pop_widget.dart';
import 'package:yw_bapp/widgets/no_data_widget.dart';
import 'package:yw_bapp/widgets/order_item_widget.dart';
import 'package:yw_bapp/widgets/safe_area_bar_widget.dart';
import 'package:yw_bapp/widgets/show_toast.dart';
import 'package:yw_bapp/widgets/withdrawal_item.dart';
import 'package:provider/provider.dart';

// 提现申请记录页面
class WithdrawalList extends StatefulWidget {
  @override
  _WithdrawalListState createState() => _WithdrawalListState();
}

class _WithdrawalListState extends State<WithdrawalList> {
  EasyRefreshController _controller;
  List<ApplyList> applayList;
  bool isLoading = true;
  int pageIndex = 1; //当前的加载页
  int totalPage = 1; //总页数
  String scoreRatio; // 承兑汇率
  String withdrawRatio; // 手续费
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    // 首次加载
    _getApplyList(context);
    
    _controller = EasyRefreshController();
  }

  @override
  Widget build(BuildContext context) {
    scoreRatio = context.watch<StoreInfoProvider>().storeInfo.scoreRatio;
    withdrawRatio = context.watch<StoreInfoProvider>().storeInfo.withdrawRatio;
    return Scaffold(
        appBar: XAppBar(
          statusBarColor: ColorUtil.color('#FFC49226'),
          bgColor: ColorUtil.color('#FFC49226'),
          title: "兑现记录",
          fun: () {
            Navigator.pop(context);
          },
        ),
        body: EasyRefresh.custom(
          enableControlFinishRefresh: false,
          enableControlFinishLoad: true,
          controller: _controller,
          header: BezierCircleHeader(
              color: ColorUtil.color("#FFFF7200"),
              backgroundColor: ColorUtil.color("#FFFFFFFF")),
          footer: BezierBounceFooter(
              color: ColorUtil.color("#FFFF7200"),
              backgroundColor: ColorUtil.color("#FFFFFFFF")),
          onRefresh: () async {
            await Future.delayed(Duration(seconds: 2), () {
              pageIndex = 1;
              _getApplyList(context, isRefresh: true);
              _controller.resetRefreshState();
            });
          },
          onLoad: () async {
            await Future.delayed(Duration(seconds: 2), () {
              pageIndex++;
              _getApplyList(context, loadMore: true);
              _controller.finishLoad(noMore: totalPage <= pageIndex);
              if (totalPage <= pageIndex) {
                //没有更多了
                ShowToast.toast(context,
                    msg: '没有更多啦', position: ToastPostion.center);
              }
            });
          },
          slivers: <Widget>[
            SliverList(
              delegate: SliverChildBuilderDelegate(
                (context, index) {
                  return !isLoading
                      ? Container(
                          height: MediaQuery.of(context).size.height,
                          decoration: BoxDecoration(
                              border: Border(
                                  top: BorderSide(
                                      color: ColorUtil.color("#0000001A")))),
                          padding: EdgeInsets.fromLTRB(15, 0, 15, 0),
                          child: orderContainer(context),
                        )
                      : null;
                },
                childCount: 1,
              ),
            ),
          ],
        ));
  }

  // 获取订单列表数据
  _getApplyList(BuildContext context, {isRefresh: false, loadMore: false}) {
    WithdrawalDao.GetNewsList(pageIndex).then((NewsListModel model) => {
          if (model.status == 200)
            {
              loadMore
                  ? applayList.addAll(model.data.applyList)
                  : applayList = model.data.applyList,
              setState(() {
                isLoading = false;
                totalPage = model.data.pageCount;
              }),
              if (!isRefresh && !loadMore) {LoadingPop.hide(context)}
            },
        });
  }

  //  今日订单区域
  Widget orderContainer(BuildContext context) {
    // return Container(
    //   child: applayList?.length != 0 ?? false
    //       ? ListView.builder(
    //           itemCount: applayList?.length ?? 0,
    //           itemBuilder: (BuildContext context, int index) {
    //             return WithdrawalItem(
    //               item: applayList[index],
    //               type: 2,
    //             );
    //           }, //
    //         )
    //       : NoData(),
    // );

    return Container(
      child: applayList?.length != 0 ?? false
          ? ListView.builder(
              itemCount: applayList?.length ?? 0,
              itemBuilder: (BuildContext context, int index) {
                return Slidable(
                  actionPane: SlidableBehindActionPane(),
                  actionExtentRatio: 0.25,
                  child: WithdrawalItem(
                    item: applayList[index],
                    type: 2,
                  ),
                  actions: <Widget>[],
                  secondaryActions: <Widget>[
                    //右侧按钮列表
                    IconSlideAction(
                      caption: '查看详情',
                      color: ColorUtil.color("#FFC49226"),
                      icon: Icons.more_horiz,
                      onTap: () => _showSnackBar(
                        detailObject.cashDetailObj(
                            applayList[index].centScorePrice,
                            scoreRatio,
                            withdrawRatio,
                            applayList[index].innerClerkId,
                            applayList[index].updatedAt),
                            applayList[index]
                      ),
                    )
                  ],
                );
              }, //
            )
          : NoData(),
    );
  }

  _showSnackBar(detailObject detailObject,ApplyList applayItem) {
    Navigator.push(
        context, MaterialPageRoute(builder: (context) => WithdrawDetail(detailObject,applayItem)));
  }
}
